[VB.Net] Typecasting generic parameters.

Posted by CFP on Stack Overflow See other posts from Stack Overflow or by CFP
Published on 2010-05-23T12:56:48Z Indexed on 2010/05/23 13:00 UTC
Read the original article Hit count: 206

Filed under:
|
|

Hello world!

Using the following code:

Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T) As T
    Return If(Configuration.ContainsKey(SettingName), CType(Configuration(SettingName), T), DefaultVal)
End Function

Yields the following error:

Value of type 'String' cannot be converted to 'T'.

Any way I could specify that in all cases, the conversion will indeed be possible (I'm basically getting integers, booleans, doubles and strings).

Thanks!

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about generics